<? 
require("adm_header.php"); 
writeCommonAdminTable("Import Tool",1);

echo "<b>Path to your current installation:</b><br />";
echo "<form method='post' action='import.php'>";
echo "<input type='text' size='70' name='path_old_db' value='".getParam("path_old_db","")."'>";
echo " <input type='submit' name='submit_check' value='Check'>";
echo "</form>";
$lDryTest=1;



if (getParam("submit_check",""))
{
	
	if (file_exists(getParam("path_old_db","")))
	{
		
		echo "<font color='green'>File exists OK</font>";
		$lOk=1;
		$lPathOld=getParam("path_old_db","");
	}
	else 
		echo "<font color='red'>Does NOT exist</font>";
		
		
	if ($lOk)
	{	
		 require "$lPathOld/admin/db.php";
		 $res_conn = mysql_connect ("$dbhost","$dbusr","$dbpass");
		 $res_sel = mysql_select_db ("$datab");
	     
		 if ($res_conn==false) 
		 {
	 		echo "<p><font color=red><b>Warning:</b></font><br />Could not connect to the database based on the information you gave. Make sure that you have
	 		correct hostname, username, password and databasename, and also make sure that your mysql server is running.</p>";
	 		$error=1;
		}
		else 
			echo "<p><font color=green><b>Connection Success: OK</b></font><br />We could connect to mysql</p>";
		
		if (!$error)
		{
			if ($res_sel=="") 
			{
		 		echo "<p><font color=red><b>Warning:</b></font><br />Could not SELECT the database you spesified: <b>$db_name</b>. 
		 		Make sure you have given the user <b>$db_username</b> enough access privileges to that database.</p>";	
				$error=1;
		 	}
		 	else 
		 		echo "<p><font color=green><b>Database Selection Success: OK</b></font><br />We could select and get access to database supplied</p>";
		}
		
		
		if (!$error)
		{
		
			// Get all e_ fields from ads
			$res=mysql_query("show fields from ad from $datab");
			while ($row=mysql_fetch_array($res))
			{
				if (ereg("^e_",$row["Field"]))
					$lF[]=$row["Field"];
			}
			
			
			$sql="select * from category";
			$res=mysql_query($sql);
			
			echo "<h2>Import of categories</h2>";
			while ($row=mysql_fetch_array($res))
			{		
				$lCatName=strToDb($row["catname"]);		
				$lCatId=$row["catid"];		
				$lCatFatherId=$row["catfatherid"];	
				$lCatDescr=strToDb($row["catdescription"]);
				$lCatImage=$row["catimage"];	
				$lAllowAds=$row["allowads"];
				
				$sql="insert into $cat_tbl (cat_id,cat_fatherid,cat_name,cat_description,cat_image,cat_allow_ads)";
				$sql.=" values ($lCatId,$lCatFatherId,'$lCatName','$lCatDescr','$lCatImage','$lAllowAds')";
				//echo "$lCatName - $lCatId - $lCatFatherId - $lCatDescr - $lCatImage - $lAllowAds";
				
				echo $sql . "<br>";
				$res2=specialq($sql);
			}
			
			require "$lPathOld/admin/db.php";
			echo "<h2>Import of users</h2>";
			$sql="select * from user";
			$res=mysql_query($sql,$res_conn);
			
			while ($row=mysql_fetch_array($res))
			{
				$lUserName=htmlspecialchars($row["name"],ENT_QUOTES);	
									
				$lUserEmail=htmlspecialchars($row["email"],ENT_QUOTES);	
				$lUserEmelding=$row["emelding"];		
				$lUserNumAds=$row["num_ads"];		
				$lUserHideEmail=$row["hide_email"];		
				$lUserPass=$row["password_enc"];		
				$lUserCredits=$row["credits"];		
				$lUserStatus=$row["status"];		
				$lUserVerify=$row["verify"];		
				$lUserLastLogin=$row["last_login"];		
				$lUserNumLogged=$row["num_logged"];		
				$lUserNumMonths=$row["months"];		
				$lUserNumContacted=$row["num_contacted"];		
				$lUserVendorUrl=$row["vendor_url"];		
				$lUserIsVendor=$row["is_vendor"];	
				$lUserVendorHomepage=htmlspecialchars($row["vendor_homepage"],ENT_QUOTES);		
				$lUserVotes=$row["votes"];		
				$lUserVoters=$row["voters"];		
				$lUserRegistered=$row["registered"];		
				$year=substr($lUserRegistered,0,4);
				$month=substr($lUserRegistered,4,2);
				$day=substr($lUserRegistered,6,2);
				$lRegTime=mktime(0,0,0,$month,$day,$year);
				
				$year=substr($lUserLastLogin,0,4);
				$month=substr($lUserLastLogin,4,2);
				$day=substr($lUserLastLogin,6,2);
				$lLastLoginTime=mktime(0,0,0,$month,$day,$year);
				
				
				
				$sql="insert into $usr_tbl (user_name,user_email,user_date,user_date_last_login,user_num_login,user_contacted,user_pass_md5,user_credits,user_status,user_votes,user_voters,user_vendor_url,user_vendor_homepage,user_is_vendor) 
				values 
				('$lUserName','$lUserEmail',$lRegTime,'$lLastLoginTime','$lUserNumLogged','$lUserNumContacted','$lUserPass','$lUserCredits','$lUserStatus','$lUserVotes','$lUserVoters','$lUserVendorUrl','$lUserVendorHomepage','$lUserIsVendor')";
				
				echo "$sql<br>";
				//if (!$lDryTest)
					$res48473=specialq($sql);
				
				
				$lNewUserID=mysql_insert_id();
				//---  Import de los creditos de Usuario   ----//
				require "$lPathOld/admin/db.php";
		 		$res_conn = mysql_connect ("$dbhost","$dbusr","$dbpass");
		 		$res_sel = mysql_select_db ("$datab");		 		
				
				echo "<h2>Imports of credits</h2>";
				$sql_credits="select * from $user_credits where user_id=$lNewUserID";
				
				$res_credits=mysql_query($sql_credits,$res_conn);
				echo mysql_numrows($res_credits);
				
				while ($row_credits=mysql_fetch_array($res_credits))
				{
					$sql="insert into $user_credits(`user_id`,`pay_membership_expire`,`pay_credits`,`pay_bold`,`pay_num_images`,`pay_upload`,
                          `pay_wysiwyg`,`pay_vendor`,`pay_premium`,`pay_upload_video`,`package_id`,`mainType`)  
				           values (".$row_credits["user_id"].",".
					                 $row_credits["pay_membership_expire"].",".
					                 $row_credits["pay_credits"].",".
					                 $row_credits["pay_bold"].",".
					                 $row_credits["pay_num_images"].",".
					                 $row_credits["pay_upload"].",".
                                     $row_credits["pay_wysiwyg"].",".
                                     $row_credits["pay_vendor"].",".
                                     $row_credits["pay_premium"].",".
                                     $row_credits["pay_upload_video"].",".
                                     $row_credits["package_id"].",".
                                     $row_credits["mainType"].");";
				
				echo "$sql<br>";
				//if (!$lDryTest)
					$resCredits=specialq($sql);
					
				}
				//---  END de Import de los creditos de Usuario   ----//
				require "$lPathOld/admin/db.php";
		 		$res_conn = mysql_connect ("$dbhost","$dbusr","$dbpass");
		 		$res_sel = mysql_select_db ("$datab");
						 		
		 		echo "<h2>Import of ads</h2>";
				$sql_ads="select * from ad where ad_username='$lUserEmail'";
				
				$res_ads=mysql_query($sql_ads,$res_conn);
				echo mysql_numrows($res_ads);
				
				while ($row2=mysql_fetch_array($res_ads))
				{
					
					$lthisAdID=$row2["siteid"];	
					$lAdTitle=strToDb($row2["sitetitle"]);		
					$lAdDescription=strToDb($row2["sitedescription"]);			
					$lAdDate=$row2["datestamp"];	
					$lAdCat=$row2["sitecatid"];	
					$lAdHits=$row2["sitehits"];	
					$lAdVotes=$row2["sitevotes"];
					$lAdVoters=$row2["sitevoters"];
					$lAdScore=round($row2["sitescore"]);
					$lAdPicture=$row2["picture"];
					$lAdUsername=$row2["ad_username"];
					$lAdExpireDays=$row2["expire_days"];
					$lAdValid=$row2["valid"];
					$lAdIsSold=$row2["sold"];
					$lAdIsSpecial=$row2["is_special"];

					if (!$lAdHits)
						$lAdHits=0;
					if (!$lAdIsSold)
						$lAdIsSold=0;					
					if (!$lAdIsSpecial)
						$lAdIsSpecial=0;					
					if (!$lAdPicture)
						$lAdPicture=0;		
					if (!$lAdVotes)
						$lAdVotes=0;
					if (!$lAdVoters)
						$lAdVoters=0;	
					if (!$lAdScore)
						$lAdScore=0;
					if (!$lAdValid)
						$lAdValid=0;							
					//echo "$lAdTitle - $lAdDescription - $lAdDate - $lAdCat - $lAdHits - $lAdVotes - $lAdVoters - $lAdPicture - $lAdUsername - $lAdExpireDays<br>";
					
					if (!$lAdExpireDays)
						$lAdExpireDays=51;
						
					$lKeys="";
					$lVals="";
					
					foreach ($lF as $lItem)
					{
						$lKeys.="," . $lItem;
						$lVals.=",'".htmlspecialchars($row2[$lItem],ENT_QUOTES) . "'";
					}
						
					$lExpireDate=addDaysToTimeStamp($lAdExpireDays,time());
					$sql="insert into $ads_tbl (ad_owner,ad_cat_id,ad_title,ad_description,ad_date,ad_date_expire,ad_expires_after_days,ad_has_picture,ad_views,ad_votes,ad_voters,ad_totalscore,ad_is_validated,ad_is_sold,ad_is_special $lKeys)";
					$sql.=" values (";
					$sql.="$lNewUserID,$lAdCat,\"$lAdTitle\",\"$lAdDescription\",".time().",$lExpireDate,$lAdExpireDays,$lAdPicture,$lAdHits,$lAdVotes,$lAdVoters,$lAdScore,$lAdValid,$lAdIsSold,$lAdIsSpecial";
					$sql.=" $lVals )";
					echo "$sql<br>";
					$res929=specialq($sql);
					$lNewAdID=mysql_insert_id();
					userAdsCounter($lNewUserID,1);	 // Increase counter for this user
					categoryCounter($lAdCat,1);	
					
					require "$lPathOld/admin/db.php";
		 			$res_conn = mysql_connect ("$dbhost","$dbusr","$dbpass");
		 			$res_sel = mysql_select_db ("$datab");
					
		 			echo "<h2>Import of Images</h2>";
					$sql_ads="select * from picture where pictures_siteid='$lthisAdID'";
					$res_pic=mysql_query($sql_ads,$res_conn);
					
					while ($row_pic=mysql_fetch_array($res_pic))
					{	
						$lFilename=htmlspecialchars($row_pic["filename"],ENT_QUOTES);
						$lMain=$row_pic["main"];
						if (!$lMain)
							$lMain=0;
						$sql="insert into $pic_tbl (pic_ad_id,filename,main) values ($lNewAdID,'$lFilename',$lMain)";
						echo "$sql<br>";
						$res39=specialq($sql);
					}

				}
				
			}
			
		}
     
     	
     
     
	}
	
		
}
writeCommonAdminTable("Upload file","");
require("adm_footer.php"); 
?>